GeTools中的JTS基础(2)
2018年5月6日
介绍JTS的使用,是对GeoTools的一个翻译。针对Testing equality of Geometry objects页
简介
JTS包含着不同的判断Geometry对象是否相同的方法。如果你正在做大量的复杂的Geometry对象比较,那么了解不同的方法直接的区别会在你的应用运行时起到很大的帮助。
提示:如果这一页你觉得又臭又长,那么最终要的一点是永远避免使用Geometry.equals( Geometry g )。使用equalsExact 或 equalsTopo代替。
方法介绍
Geometry.equalsExact( Geometry g ):
这个方法测试Geometry对象结构的相等。简单来说,这意味着它们必须有相同数量的顶点,在相同的位置和相同的顺序中。后一种情况是棘手的。如果两个多边形,相互比较,他们的定点相同但是两个顺序正好相反那么就会输出错误。了解这一点很重要,因为当对象存储在数据存储中并随后从数据存储中检索时,顶点顺序可能发生变化。
Geometry.equalsExact( Geometry g, double tolerance )
这与前面的方法一样,但允许您为比较顶点坐标指定一个容差。
Geometry.equalsNorm( Geometry g )
这个方法让你从节点的顺序问题中解放了出来,通过几何对象的规范化(即,将每一种形式变成标准或规范形式在比较之前)。它与下面是相等的:
1 | geomA.normalize(); |
它能够保证顺序一致,但是代价是昂贵的需要额外计算。
Geometry.equalsTopo( Geometry g )
This method tests for topological equality which is equivalent to drawing the two Geometry objects and seeing if all of their component edges overlap. It is the most robust kind of comparison but also the most computationally expensive.
Geometry.equals( Object o )
This method is a synonym for Geometry.equalsExact and lets you use Geometry objects in Java Collections.
Geometry.equals( Geometry g )
This method is a synonym for Geometry.equalsTopo. It should really come with a health warning because its presence means that you can unknowingly be doing computationally expensive comparisons when quick cheap ones are all you need. For example:
Geometry geomA = ...
Geometry geomB = ...
// If geomA and geomB are complex, this will be slow:
boolean result = geomA.equals( geomB );
// If you know that a structural comparison is all you need, do
// this instead:
result = geomA.equalsExact( geomB );
The best thing approach you can take with this method is vow never to use it.